package com.aboverock.module.system.utils;

import com.aboverock.core.spring.SpringContextHolder;
import com.aboverock.module.system.domain.SuperAdmin;
import com.aboverock.module.system.domain.User;

/**
 * 用户信息工具类.
 *
 * @author Rock Wang
 */
public class UserInfoUtil {

    /** 注册本地用户时，登录名添加的后缀. */
    public static final String LOCAL_USER_LOGIN_NAME_SUFFIX = "[local]";

    /**
     * 注册本地用户时，登录名添加后缀 {@link UserInfoUtil#LOCAL_USER_LOGIN_NAME_SUFFIX}.
     *
     * @param loginName the login name
     * @return the string
     */
    public static String addSuffix2LocalUserLoginName(String loginName) {
        if (null == loginName || isSuperAdminName(loginName)) {
            return loginName;
        }
        return (loginName + LOCAL_USER_LOGIN_NAME_SUFFIX).toLowerCase();
    }

    /**
     * 根据登陆名判断是否是超级管理员.
     *
     * @param user the user
     * @return true, if is super admin
     */
    public static boolean isSuperAdmin(User user) {
        SuperAdmin superAdmin = SpringContextHolder.getBean(SuperAdmin.class);
        return superAdmin.getName().equalsIgnoreCase(user.getLoginName());
    }

    /**
     * 检测是否是超级管理员名.
     *
     * @param loginName the login name
     * @return true, if is super admin name
     */
    public static boolean isSuperAdminName(String loginName) {
        SuperAdmin superAdmin = SpringContextHolder.getBean(SuperAdmin.class);
        return superAdmin.getName().equalsIgnoreCase(loginName);
    }

}
